package com.zdx.yswl.ui.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.zdx.yswl.App;
import com.zdx.yswl.Constant;
import com.zdx.yswl.R;
import com.zdx.yswl.http.KAsyncTask.TaskDelegate;
import com.zdx.yswl.tool.KStringTool;
import com.zdx.yswl.ui.base.BaseActivity;
import com.zdx.yswl.ui.exception.NetworkException;
import com.zdx.yswl.ui.task.LoginTask;
import com.zdx.yswl.ui.task.RegTask;

public class LoginActivity extends BaseActivity implements OnClickListener, TaskDelegate
{

	private static final int LOGIN_ID = 11001;
	private static final int REG_ID = 11002;
	private EditText mTelNumber;
	private EditText mPwd;

	private String userName;
	private String userPwd;
	private Button mReg;
	private Button mLogin;

	@Override
	protected void initSettings() {

	}

	@Override
	protected void initUi() {

	}

	@Override
	protected void saveSettings() {

	}

	@Override
	protected void freeMe() {

	}

	@Override
	public void onCreate(Bundle savedInstanceState)
    {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_login);
		setHeader(new View.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				exitApp();
			}
		},"登录");
		
		
		if (App.getInstance().isLogin(this) == true) {
			startActivity(new Intent(this, MainActivity.class));
			finish();
		}
		
		mTelNumber = (EditText) findViewById(R.id.tel_nmb_et);
		mTelNumber.setFocusable(true);
		mTelNumber.requestFocus();
		mPwd = (EditText) findViewById(R.id.pwd_et);
		
		mReg = (Button) findViewById(R.id.reg_btn);
		mReg.setOnClickListener(this);
		mLogin = (Button) findViewById(R.id.login);
		mLogin.setOnClickListener(this);
		TextView mFoundPwd = (TextView) findViewById(R.id.forgit_pwd);
		mFoundPwd.setOnClickListener(this);
	
	}

	/**友盟session的统计*/
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	/**友盟session的统计*/
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
	@Override
	public void onClick(View v) {
		userName = mTelNumber.getText().toString().trim();
		userPwd = mPwd.getText().toString().trim();
		switch (v.getId()) {
		case R.id.forgit_pwd:
			startActivity(ForgetPwdActivity.actionStart(this, "忘记密码"));
			break;
		case R.id.reg_btn:
			startActivity(ForgetPwdActivity.actionStart(this, "注册"));
			break;
		case R.id.login:
			if (KStringTool.isNotEmpty(userName) && KStringTool.isNotEmpty(userPwd))
			{
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("phone", userName);
				map.put("passwd", userPwd);
				new LoginTask(LoginActivity.this,LOGIN_ID, this).execute(map);
			}
			break;
		default:
			break;
		}
		
	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
		if (object instanceof NetworkException) {
//			ConfirmDialog.share(this, -1, new ConfirmDialogDelegate() {
//				@Override
//				public void onClick_ConfirmDialog(int __id, int __position) {
//					finish();
//				}
//			});
//			ConfirmDialog.showDialog("提示", ((NetworkException) object).getMessage(), "确定");
//			return; 
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
			return ;
		}
		
		if (taskId == LOGIN_ID) {
			Toast.makeText(this, "用户不存在或者密码不正确", Toast.LENGTH_SHORT).show();
		}
		else if(taskId == REG_ID){
			Toast.makeText(this, "该手机号码已经注册", Toast.LENGTH_SHORT).show();
		}
		
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		App.getInstance().recordUserLoginStatus(this, userName, userPwd);
		if (taskId == LOGIN_ID) {
			Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
		}else if(taskId == REG_ID){
			Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
		}
		startActivity(new Intent(this, MainActivity.class));
		finish();
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitApp();
		}
		return true;
	}

	@Override
	public void onNotify(int what, int arg1, int arg2, Object obj)
	{

	}

	@Override
	public void onHandler(int what, int arg1, int arg2, Object obj)
	{

	}
}
